package com.zmm.product.controller;

import com.zmm.common.base.model.Result;
import com.zmm.common.base.model.ResultCode;
import com.zmm.common.exception.handle.BusinessException;
import com.zmm.product.model.Product;
import com.zmm.product.service.ProductService;
import com.zmm.product.vo.ProductVo;
import com.zmm.util.verify.BindingResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * author 900045
 * description:
 * name ProductController
 * dates By 2021-07-07 15:16:51
 */
@Controller
@Slf4j
@RequestMapping("/product")
public class ProductController {

	@Autowired
	private ProductService productService;

	/**
	 * 去到这个 商品新增页面
	 * @author: 900045
	 * @date: 2021-07-07 15:25:59
	 * @throws 
	
	 * @return: java.lang.String
	 **/
	@RequestMapping(value = "/to/add",method = RequestMethod.GET)
	public String toAddProduct(){
		return "add_product";
	}

	/**
	 * 新增商品 - 控制层
	 * @author: 900045
	 * @date: 2021-07-07 15:25:38
	 * @throws 
	 * @param product: 
	 * @param result: 
	 * @return: com.zmm.common.base.model.Result
	 **/
	@ResponseBody
	@RequestMapping(value = "/do/add",method = RequestMethod.POST)
	public Result addProduct(Product product, BindingResult result){
		// 1.表达校验
		if (result.hasErrors()){
			//  Model model
			//model.addAttribute("orders",result)
			return BindingResultUtil.paramVerify(result);
		}
		try {
			boolean flag = productService.addProduct(product);
			if (!flag){
				// 新增失败
				return Result.error(ResultCode.INSERT_FAILED);
			}
		} catch (BusinessException e) {
			log.error("SAVE-PRODUCT-[addProduct]  end: e-{}", e);
			return Result.error(e.getCode().getCode(),e.getMessage());
		} catch (Exception e){
			log.error("SAVE-PRODUCT  end: e-{}", e);
		}
		return Result.ok();
	}

	/**
	 * 去到这个 商品审核页面
	 * @author: 900045
	 * @date: 2021-07-07 15:56:41
	 * @throws 
	 * @param model: 
	 * @param productId: 
	 * @return: java.lang.String
	 **/
	@RequestMapping(value = "/to/audit_status",method = RequestMethod.GET)
	public String toAuditProduct(Model model,Integer productId){
		Product product = productService.findOneProductByProductId(productId);
		model.addAttribute("product",product);
		return "audit_product";
	}

	/**
	 * 审核商品 - 控制层
	 * @author: 900045
	 * @date: 2021-07-07 16:00:26
	 * @throws 
	 * @param product: 
	 * @return: com.zmm.common.base.model.Result
	 **/
	@ResponseBody
	@RequestMapping(value = "/do/audit_status",method = RequestMethod.POST)
	public Result doProductAuditStatus(Product product,BindingResult result){
		// 1.表达校验
		if (result.hasErrors()){
			//  Model model
			//model.addAttribute("orders",result)
			return BindingResultUtil.paramVerify(result);
		}
		try {
			boolean flag = productService.modifyProductAuditStatus(product.getAuditState(),product.getProductId());
			if (!flag){
				// 更新审核状态失败
				return Result.error(ResultCode.UPDATE_FAILED);
			}
		} catch (BusinessException e) {
			log.error("UPDATE-PRODUCT-[doProductAuditStatus]  end: e-{}", e);
			return Result.error(e.getCode().getCode(),e.getMessage());
		} catch (Exception e){
			log.error("UPDATE-PRODUCT  end: e-{}", e);
		}
		return Result.ok();
	}

	/**
	 * 根据 vo 查询商品信息列表
	 * @author: 900045
	 * @date: 2021-07-07 16:55:53
	 * @throws 
	 * @param model: 
	 * @return: java.lang.String
	 **/
	@RequestMapping(value = "/query/by/vo",method = RequestMethod.GET)
	public String queryProductList(Model model){
		ProductVo productVo = new ProductVo();
		List<Product> productList = productService.queryProductListByProductVo(productVo);
		model.addAttribute("productList",productList);
		return "product_list";
	}


	/**
	 * 根据 商品ID 查看详情 -- 控制层
	 * @author: 900045
	 * @date: 2021-07-07 16:30:32
	 * @throws 
	 * @param model: 
	 * @param productId: 
	 * @return: java.lang.String
	 **/
	@RequestMapping(value = "/find/productId",method = RequestMethod.GET)
	public String viewProduct(Model model,Integer productId){
		Product product = productService.findOneProductByProductId(productId);
		model.addAttribute("product",product);
		return "product_view";
	}


	/**
	 * 根据商品ID 删除商品 -- 控制层
	 * @author: 900045
	 * @date: 2021-07-07 16:48:57
	 * @throws 
	 * @param productId: 
	 * @return: java.lang.String
	 **/
	@RequestMapping(value = "/remove/productId",method = RequestMethod.GET)
	public String removeProduct(Integer productId){
		try {
			boolean flag = productService.removeProductByProductId(productId);
			if (flag){
				return "product_list";
			} else {
				return "error";
			}
		} catch (BusinessException e) {
			return "error";
		}
	}

	/**
	 * 去到这个 商品更新页面
	 * @author: 900045
	 * @date: 2021-07-07 16:41:27
	 * @throws 
	 * @param model: 
	 * @param productId: 
	 * @return: java.lang.String
	 **/
	@RequestMapping(value = "/to/update",method = RequestMethod.GET)
	public String toUpdate(Model model,Integer productId){
		Product product = productService.findOneProductByProductId(productId);
		model.addAttribute("product",product);
		return "product_update";
	}

	/**
	 * 商品更新 -- 控制层
	 * @author: 900045
	 * @date: 2021-07-07 16:41:23
	 * @throws 
	 * @param product: 
	 * @param result: 
	 * @return: com.zmm.common.base.model.Result
	 **/
	@RequestMapping(value = "/do/update",method = RequestMethod.POST)
	public Result doUpdate(Product product,BindingResult result){
		// 1.表达校验
		if (result.hasErrors()){
			//  Model model
			//model.addAttribute("orders",result)
			return BindingResultUtil.paramVerify(result);
		}
		try {
			boolean flag = productService.modifyProduct(product);
			if (!flag){
				// 更新审核状态失败
				return Result.error(ResultCode.UPDATE_FAILED);
			}
		} catch (BusinessException e) {
			log.error("UPDATE-PRODUCT-[doUpdate]  end: e-{}", e);
			return Result.error(e.getCode().getCode(),e.getMessage());
		} catch (Exception e){
			log.error("UPDATE-PRODUCT  end: e-{}", e);
		}
		return Result.ok();
	}
	
}
